package name.jautzy.esi.server;

import java.util.LinkedList;
import java.util.List;

import name.jautzy.esi.client.model.*;
import name.jautzy.cedb.model.*;
import name.jautzy.esi.client.service.EveDatabaseService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class EveDatabaseServiceImpl extends RemoteServiceServlet implements
		EveDatabaseService {

	private EdbDAO dao = new EdbDAO();
	
	@Override
	public List<EdbObject> getCategories() {
		List<EdbObject> retList= new LinkedList<EdbObject>();
		
		for (Category cat : dao.getCategories()) 
			retList.add(new EdbCategory(cat.getCatName(),cat.getGraphicsId(), cat.getCatId()));
		
		return retList;
	}
	
	@Override
	public List<EdbObject> getGroups(EdbCategory parent) {		
		List<EdbObject> retList= new LinkedList<EdbObject>();
		
		for (Group group : dao.getGroups(parent.getId()))
			retList.add(new EdbGroup(group.getGroupName(),group.getGraphicsId(), group.getGroupId()));
		
		return retList;
	}
	
	@Override
	public List<EdbGroup> getShipGroups() {
		List<EdbGroup> retList= new LinkedList<EdbGroup>();
		
		for (Group group : dao.getShipGroups())
			retList.add(new EdbGroup(group.getGroupName(),group.getGraphicsId(), group.getGroupId()));
		
		return retList;
		
	}

	@Override
	public List<EdbObject> getObjects(EdbObject parent) {
		if (parent==null)
			return getCategories();
		if (parent instanceof EdbCategory)
			return getGroups((EdbCategory)parent);
		if (parent instanceof EdbGroup)
			return getEquipments((EdbGroup)parent);
		return null;
	}

	@Override
	public List<EdbObject> getEquipments(EdbGroup parent) {
		List<EdbObject> retList= new LinkedList<EdbObject>();
		
		for (Equipment equip : dao.getEquipments(parent.getId())) {
			EdbEquipment edbEquip= new EdbEquipment(equip.getName(),0,equip.getEqId());
			edbEquip.setCapacity(equip.getCapacity());
			edbEquip.setMass(equip.getMass());
			edbEquip.setRace(equip.getRace());
			edbEquip.setRadius(equip.getRadius());
			edbEquip.setVolume(equip.getVolume());
			edbEquip.setDescription(equip.getDescription());
			edbEquip.setIcon(equip.getIcon());
			retList.add(edbEquip);
		}
		
		return retList;
	}

	@Override
	public List<EdbAttribute> getAttributes(EdbEquipment parent) {
		List<EdbAttribute> retList= new LinkedList<EdbAttribute>();
		
		for (Attribute a : dao.getAttributes(parent.getId())){
			EdbAttribute att = new EdbAttribute();
			att.setName(a.getName());
			att.setValue(a.getValue());
			retList.add(att);
		}
		
		return retList;
	}

	@Override
	public List<EdbEffect> getEffects(EdbEquipment parent) {
		List<EdbEffect> retList= new LinkedList<EdbEffect>();
		
		for(Effect a : dao.getEffects(parent.getId())){
			EdbEffect att = new EdbEffect();
			att.setName(a.getName());
			att.setOffensive(a.getIsOffensive());
			att.setAssistance(a.getIsAssistance());
			retList.add(att);
		}
		
		return retList;
	}
	
	@Override
	public List<EdbBonus> getBonus(EdbEquipment parent) {
		List<EdbBonus> retList= new LinkedList<EdbBonus>();
		
		for (Bonus a : dao.getBonus(parent.getName())){
			EdbBonus att = new EdbBonus();
			att.setName(a.getBonusName());
			att.setValue(a.getValue());
			att.setId(a.getBonusId());
			att.setSkill(a.getSkill());
			retList.add(att);
		}
		
		return retList;
	}

	@Override
	public void updateBonus(List<EdbBonus> bonuses, EdbEquipment parent) {
		for (EdbBonus b : bonuses) {
			Bonus bonus = new Bonus();
			bonus.setBonusId(b.getId());
			bonus.setShipName(parent.getName());
			bonus.setBonusName(b.getName());
			bonus.setValue(b.getValue());
			bonus.setSkill(b.getSkill());
			dao.putBonus(bonus);
		}
	}

	@Override
	public void removeBonus(List<EdbBonus> bonuses) {
		for (EdbBonus b :bonuses) {
			Long id = b.getId();
			if (id!=null) {
				Bonus bonus = dao.getBonus(id);
				if (bonus!=null)
					dao.removeBonus(bonus);
			}
				
		}
		
	}
	
	

}
